﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ShareB.Models;

namespace ShareB.Controllers
{
    public class PublicationController : Controller
    {
        //serviços necessários 
        PublicationService pub_serv = new PublicationService();
        PubConfService conf_serv = new PubConfService();
        IMembershipService user_serv = new UserMembershipService();

        //
        // GET: /Publication/

        public ActionResult Index()
        {
            return View();
        }

        //
        // GET: /Publication/Details/5

        public ActionResult Details(int id)
        {
            return View();
        }

        //
        // GET: /Publication/Create

        public ActionResult Create()
        {
            return View();
        } 

        //
        // POST: /Publication/Create

        [HttpPost]
        public ActionResult Create(FormCollection collection)
        {
            try
            {
                // TODO: Add insert logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
        
        //
        // GET: /Publication/Edit/5
 
        public ActionResult Edit(int id)
        {
            return View();
        }

        //
        // POST: /Publication/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add update logic here
 
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Publication/Delete/5
 
        public ActionResult Delete(int id)
        {
            return View();
        }

        //
        // POST: /Publication/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here
 
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        // **************************************
        // Get
        // URL: /Publications/ChoiceConfiguration
        // **************************************

        public ActionResult ChoiceConfiguration()
        {
            IList<PubConfModel> model = new List<PubConfModel>();
            PubConfService modelserv = new PubConfService();
            model = modelserv.GetAllConfigurations();

            return View(model);
        }

        // **************************************
        // Get
        // URL: /Publications/ViewMyPub
        // **************************************

        public ActionResult ViewAllMyPub()
        {
            IList<PublicationModel> model = new List<PublicationModel>();
            PublicationService modelserv = new PublicationService();
            model = modelserv.GetAllMyPublication();

            return View(model);
        }

        // **************************************
        // GET
        // URL: /Publications/ViewMyPub
        // **************************************
        public ActionResult ViewMyPub(PublicationModel model)
        {
            //pub_serv.ViewMyPub(Convert.ToInt32(id));

            return View(model);
        }

        // **************************************
        // Get
        // URL: /Publications/CreatePublication
        // **************************************
        
        public ActionResult CreatePublication(string id)
        {
            NewPublicationModel model = new NewPublicationModel();
            model.configuration = conf_serv.GetConfigurations(Convert.ToInt32(id));
            foreach (MetaFieldModel m in model.configuration.fieldsConf)
            {
                    model.Fields.Add(new FieldModel
                    {
                        FieldName = m.FieldName,
                        MetaFieldID = m.MetaFieldID
                    });
            }
            model.Tittle = "EsteCampoNaoMuitoNecessario";
            model.PubConfID = Convert.ToInt32(id);
            model.UserID = user_serv.GetUserID(HttpContext.User.Identity.Name);
            model.Username = HttpContext.User.Identity.Name;
            model.StartDate = System.DateTime.Now;

            return View(model);
        }

        // **************************************
        // POST
        // URL: /Publications/CreatePublication
        // **************************************
        [HttpPost]
        public ActionResult CreatePublication(NewPublicationModel model)
        {
            PublicationModel m = (PublicationModel)model;
            pub_serv.CreatePublication(m,m.PubConfID);

            return RedirectToAction("Index");
        }
    }
}
